package com.netease.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.netease.jichu.Product;
import com.netease.jichu.User;
import com.netease.service.ProductService;
import com.netease.service.UserDaoService;

@Controller
public class UserController {
	@Autowired
	private UserDaoService us;
	@Autowired
	private ProductService ps;
	
	@RequestMapping(value="/login")
	public String login(){
		return "login";
	}
	@RequestMapping(value="/api/login",method=RequestMethod.POST)
	public @ResponseBody Object loginInfo(@RequestParam(value="userName") String userName,
			@RequestParam(value="password" ) String password,
			Model model,HttpServletRequest request,HttpServletResponse response)throws IOException{
			User user=us.login(userName, password);
			if(user==null){
				System.out.println(user.getNickName());
				return user;
//				user= us.getUserInfo(2);
//				model.addAttribute("code",200);
//				request.getSession().setAttribute("userName",userName);
//				request.getSession().setAttribute("user",user);
//				model.addAttribute("user",user);
			}
			else{
//				user= us.getUserInfo(2);
//				System.out.println(user.getNickName());
				model.addAttribute("code",200);
				request.getSession().setAttribute("user",user);
				model.addAttribute("user",user);
				List<Product> product=ps.productAll();
				model.addAttribute("productList",product);
				return model;
			}
		
			
//			model.addAttribute("items",us.getUserInfo(use));
			
//			map.addAttribute("message","登录成功");
//			model.addAttribute("result",true);
//			String nick=user.getNickName();
//			int userType =user.getUsertype();
//			model.addAttribute("user",user);
//			model.addAttribute("usertype",user.getUsertype());
//			List<Product> product=ps.productAll();
//			model.addAttribute("productList",product);
//			System.out.println(userName+":"+password+":"+user.getNickName()+" "+user.getUsertype());
//			
		}
//		else{
//			//跳转登录页面
//			System.out.println("请重新登录");
//			return login();
//		}
//		}
		
//		return model;
//	}

	@RequestMapping(value="/logout")
	public String logout(){
		return "login";
	}
	@RequestMapping("/test")
	public void test(ModelMap map,User user){
		user=us.getUserInfo(2);
		map.addAttribute(user);
	}
}
